import { ref } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';

export function useGetList() {
  const store = useStore();
  const router = useRouter();

  const searchData = ref({
    searchType: 'assetVul',
    searchCondition: '',
    pageSize: 10,
    pageNumber: 1,
    holdingRatio: '',
    orderCondition: [
      // {
      //   name: 'exposure_risk', // 暴露风险
      //   order: 'desc',
      //   label: '暴露风险',
      // },
      {
        name: 'update_time', // 更新时间
        order: 'desc',
        label: '更新时间',
      },
    ],
  });
  const total = ref(0);
  const publicList = ref([]);
  const loading = ref(false);
  const tabsList = ref([]);
  const tabsData = ref({});
  const publicData = ref({});
  const leftLoading = ref(false);

  const getList = async () => {
    loading.value = true;
    publicList.value = [];
    const { code, data = {} } = await store.dispatch(
      'attack/getList',
      searchData.value,
    );
    loading.value = false;
    if (code !== 0) {
      return;
    }
    const { dataPage = {}, searchTypeQuantity = {} } = data;
    dataPage.records?.forEach((el: any) => (el.isChecked = false));
    publicList.value = dataPage.records;
    total.value = dataPage.total;
    tabsData.value = searchTypeQuantity;
    await getExposedTypes(searchTypeQuantity);
  };

  const getExposedTypes = async (tabsData: any) => {
    const { code, data = [] } = await store.dispatch('attack/getExposedTypes');
    if (code !== 0) {
      return;
    }
    data.forEach((item) => {
      item.num = tabsData[item.typeCode];
    });
    tabsList.value = data;
  };

  const handleSizeChange = (v: any) => {
    searchData.value.pageSize = v;
    getList();
  };

  const handleCurrentChange = (v: any) => {
    searchData.value.pageNumber = v;
    getList();
  };
  const blur = (val: any) => {
    searchData.value.holdingRatio = val;
    getLeftList();
    getList();
  };

  const successFn = () => {
    setTimeout(tabClick, 3000);
  };

  const searchClick = (val: any) => {
    searchData.value.searchCondition = val;
    tabClick(false);
  };
  // 左侧树结构拼接条件
  const checkClick = (res: any, boolean: boolean) => {
    connectStr(res, boolean, 'tree');
  };

  const exposureHandle = (res: any, boolean: boolean) => {
    connectStr(res, boolean, 'left');
  };

  const connectStr = (res: any, boolean: boolean, type: string) => {
    let mark = '&&';
    if (type === 'tree') {
      mark = '||';
    } else {
      mark = '&&';
    }
    // already exists
    if (boolean) {
      if (searchData.value.searchCondition.includes(`${mark}${res}`)) {
        searchData.value.searchCondition =
          searchData.value.searchCondition.replace(`${mark}${res}`, '');
      } else {
        searchData.value.searchCondition =
          searchData.value.searchCondition.replace(`${res}`, '');
      }
    } else if (searchData.value.searchCondition) {
      searchData.value.searchCondition += `${mark}${res}`;
    } else {
      searchData.value.searchCondition = res;
    }
    if (searchData.value.searchCondition.substring(0, 2) == mark) {
      searchData.value.searchCondition =
        searchData.value.searchCondition.substring(
          2,
          searchData.value.searchCondition.length,
        );
    }

    tabClick(false);
  };

  const tabClick = (activeName: any) => {
    if (activeName) {
      searchData.value.searchType = activeName;
    }
    searchData.value.pageNumber = 1;
    getLeftList();
    getList();
  };

  const getLeftList = async () => {
    leftLoading.value = true;
    const { code, data = {} } = await store.dispatch(
      'attack/getLeftList',
      searchData.value,
    );
    leftLoading.value = false;
    if (code !== 0) {
      return;
    }
    publicData.value = data;
  };

  const clickItem = (item: any) => {
    const { businessType, id } = item;

    const address = {
      assetVul: '/Assault/AttackList/assetVulDetail',
    };

    const path = address[businessType];

    if (path) {
      router.push({
        path,
        query: {
          id,
        },
      });
    }
  };

  return {
    searchData,
    total,
    publicList,
    loading,
    tabsList,
    tabsData,
    publicData,
    leftLoading,
    tabClick,
    handleSizeChange,
    handleCurrentChange,
    getList,
    checkClick,
    exposureHandle,
    clickItem,
    blur,
    successFn,
    searchClick,
    getLeftList,
  };
}
